<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="rightinfo">
    <ul class="seachform">
    <li>
        <label>资源类型</label>
        <select id="type" class="selectmenu">        
            <option value="menu" selected="selected">菜单</option>        
            <option value="api">API</option>
        </select>
    </li>
    <li>
        <label>角色</label>
        <select id="role_id" class="selectmenu">        
            <option value="2" selected="selected">系统管理员</option>        
            <option value="1">超级管理员</option>
        </select>
    </li>
    </ul>
    <div id="tablelist">
    
    </div>    
    <div class="clearfloat"></div>
</div>


<!-- 模板 -->
<script id="list" type="text/html">
    <table class="tablelist">
        <thead>
        <tr>
        <th style="width:8%">编号</th>
        <th>URI</th>
        <th style="width:25%">功能描述</th>
        <th style="width:16%">角色</th>
        <th style="width:16%">权限</th>
        <th style="width:16%">操作</th>
        </tr>
        </thead>
        <tbody>
        <%for(i = 0; i < list.length; i ++) {%>
            <tr <%=#(i%2 ==0) ?'':'class="odd"'%> data-uri="<%=list[i].uri%>">
            <td><%=list[i].id%></td>
            <td><%=list[i].uri%></td>
            <td><%=list[i].description%></td>
            <td><%=role_name%></td>
            <td><%=#list[i].permission?'有':'<i>无</i>'%></td>
            <td>
                <a class="tablelink act_edit"><%=list[i].permission?'取消':'设置'%></a>
            </td>
            </tr>
        <%}%>        
        </tbody>
    </table>  
</script>

<!-- 脚本 -->
<script src="js/template-helper.js" type="text/javascript"></script>
<script type="text/javascript">
function get_page(){
    $.getJSON(
          '<?php echo site_url('ajax/permission_handle/get_list');?>',
          {
        	  role_id:$('#role_id').val(),
        	  type:$('#type').val()
          },
          function(rs){
              if(rs.err===0){
                $('#tablelist').html(template('list', $.extend({},{"role_name":$('#role_id').find("option:selected").text()},rs.data)));                
              }else{
                  layer.msg(rs.msg, {icon:2,time:2000});
              }            
        }
    );
}
//操作
$('#tablelist').on('click', '.act_edit', function(){
	var modify_data = {};
    modify_data.uri = $(this).parent().parent().attr('data-uri');
    modify_data.role_id = $('#role_id').val();
    modify_data.uri_type = $('#type').val()=='menu'?1:0;
    modify_data.flag = $(this).text()=='设置'?1:0;
    $.post(
            '<?php echo site_url('ajax/permission_handle/modify');?>',
            modify_data,
            function(rs){
                if(rs.err===0){
                    layer.msg('修改成功！', {icon:1,time:2000});    
                    get_page();
                }else{
                    layer.msg(rs.msg, {icon:2,time:2000});
                }
            },
            'json'
    );
});

$( ".selectmenu" ).selectmenu({width:162, change:function(event, data){
	get_page();
}});
get_page();
</script>